package it.rikesh.aip2011.server.service;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

import org.slim3.datastore.Datastore;

import com.google.appengine.api.datastore.Key;

import it.rikesh.aip2011.client.service.ActivityService;
import it.rikesh.aip2011.server.meta.ActivityMeta;
import it.rikesh.aip2011.shared.model.Activity;
import it.rikesh.aip2011.shared.model.Hut;
import it.rikesh.aip2011.shared.model.HutActivity;

/**
 * @author  RN
 */
public class ActivityServiceImpl implements ActivityService {
    private static final Logger log = Logger.getLogger(ActivityServiceImpl.class.getName());
    private ActivityMeta h = new ActivityMeta();
    
    public String getActivityDetail(String id) {
        Key key=Datastore.createKey(Activity.class, Long.parseLong(id));
        Activity activity=Datastore.query(h)
            .filter(h.key.equal(key))
            .asSingle();
        return activity.getDetail().getValue();
    }

    public String getActivityImg(String id) {
        Key key=Datastore.createKey(Activity.class, Long.parseLong(id));
        Activity activity=Datastore.query(h)
            .filter(h.key.equal(key))
            .asSingle();
        return activity.getImg();
    }

    public List<Activity> getActivityList() {
        return Datastore.query(h).asList();
    }

    public String getActivityName(String id) {
        Key key=Datastore.createKey(Activity.class, Long.parseLong(id));
        Activity activity=Datastore.query(h)
            .filter(h.key.equal(key))
            .asSingle();
        return activity.getName();
    }

    public String getActivityTitle(String id) {
        Key key=Datastore.createKey(Activity.class, Long.parseLong(id));
        Activity activity=Datastore.query(h)
            .filter(h.key.equal(key))
            .asSingle();
        return activity.getTitle();
    }

    public String getActivityType(String id) {
        Key key=Datastore.createKey(Activity.class, Long.parseLong(id));
        Activity activity=Datastore.query(h)
            .filter(h.key.equal(key))
            .asSingle();
        return activity.getType();
    }

    public List<Hut> getHutActivity(String id){
        Key key=Datastore.createKey(Activity.class, Long.parseLong(id));
        Activity activity=Datastore.query(h)
            .filter(h.key.equal(key))
            .asSingle();
        List<Hut> list=new ArrayList<Hut>();
        for (HutActivity ha : activity.getHutActivityListRef().getModelList()) {
            list.add(ha.getHutRef().getModel());
        }
        return list;
    }
    
    public String getActivityEquip(String id) {
        Key key=Datastore.createKey(Activity.class, Long.parseLong(id));
        Activity activity=Datastore.query(h)
            .filter(h.key.equal(key))
            .asSingle();
        return activity.getEquip();
    }
    
    public String getActivityId(String name){
        Activity activity=Datastore.query(ActivityMeta.get())
            .filter(ActivityMeta.get().name.equal(name))
            .asSingle();
        log.info("Query getActivityId("+name+") eseguito");
        return String.valueOf(activity.getKey().getId());
        
    }
    
    public List<Activity> getActivityByCategory(String category){
        return Datastore.query(ActivityMeta.get())
        .filter(ActivityMeta.get().type.equal(category))
        .asList();
    }
    
    public String getActivityTrail(String id) {
        Key key=Datastore.createKey(Activity.class, Long.parseLong(id));
        Activity activity=Datastore.query(h)
            .filter(h.key.equal(key))
            .asSingle();
        return activity.getTrailRef().getModel().getName();
    }
}
